<?php

class GroupController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
        return array(
			// page action renders "static" pages stored under 'protected/views/admin/pages'
			// They can be accessed via: index.php?r=admin/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/admin/index.php'
		// using the default layout 'protected/views/layouts/main.php'
       $model= new GroupComponent;
//        $criteria=new CDbCriteria;
//
//        $criteria->compare('name', $model->name, true);
//        $criteria->compare('disabled', $model->disabled, true);
//        $criteria->compare('description', $model->description, true);

       $dataProvider = new CActiveDataProvider('UserGroup', array(
                                                'criteria'=>  array(
                                                    'condition'=>"name like '%".$model->name."%'",
                                                ),
                                                'pagination'=>array(
                                                    'pageSize'=>2,
                                                ),
                                                'sort'=>array(
                                                    'defaultOrder'=> array('title'=>false),
                                                )
                                                
                                            ));
        $this->render('index',array(
                'dataProvider'=>$dataProvider,
                'model'=>$model
        ));

	}
    public function actionArray()
    {
        $yiiDevelopers = array(
        array(
        'name'=>'Qiang Xue',
        'id'=>'2',
        'forumName'=>'qiang',
        'memberSince'=>'Jan 2008',
        'location'=>'Washington DC, USA',
        'duty'=>'founder and project lead',
        'active'=>true,
        ),
        array(
        'name'=>'Wei Zhuo',
        'id'=>'3',
        'forumName'=>'wei',
        'memberSince'=>'Jan 2008',
        'location'=>'Sydney, Australia',
        'duty'=>'project site maintenance and development',
        'active'=>true,
        ),
        array(
        'name'=>'Sebastián Thierer',
        'id'=>'54',
        'forumName'=>'sebas',
        'memberSince'=>'Sep 2009',
        'location'=>'Argentina',
        'duty'=>'component development',
        'active'=>true,
        ),
        array(
        'name'=>'Alexander Makarov',
        'id'=>'415',
        'forumName'=>'samdark',
        'memberSince'=>'Mar 2010',
        'location'=>'Russia',
        'duty'=>'core framework development',
        'active'=>true,
        ),
        array(
        'name'=>'Maurizio Domba',
        'id'=>'2650',
        'forumName'=>'mdomba',
        'memberSince'=>'Aug 2010',
            'location'=>'Croatia',
        'duty'=>'core framework development',
        'active'=>true,
        ),
        array(
        'name'=>'Y!!',
        'id'=>'1644',
        'forumName'=>'Y!!',
        'memberSince'=>'Aug 2010',
        'location'=>'Germany',
        'duty'=>'core framework development',
        'active'=>true,
        ),
        array(
        'name'=>'Jeffrey Winesett',
        'id'=>'15',
        'forumName'=>'jefftulsa',
        'memberSince'=>'Sep 2010',
        'location'=>'Austin, TX, USA',
        'duty'=>'documentation and marketing',
        'active'=>true,
        ),
        array(
        'name'=>'Jonah Turnquist',
        'id'=>'127',
        'forumName'=>'jonah',
        'memberSince'=>'Sep 2009 - Aug 2010',
        'location'=>'California, US',
        'duty'=>'component development',
        'active'=>false,
        ),
        array(
        'name'=>'István Beregszászi',
        'id'=>'1286',
        'forumName'=>'pestaa',
        'memberSince'=>'Sep 2009 - Mar 2010',
        'location'=>'Hungary',
        'duty'=>'core framework development',
        'active'=>false,
        ),
        );
        $dataProvider = new CArrayDataProvider(
        $yiiDevelopers, array(
        'sort'=>array(
        'attributes'=>array('name', 'id', 'active'),
        'defaultOrder'=>array('active' => true, 'name' => false),
        ),
        'pagination'=>array(
        'pageSize'=>10,
        ),
        ));
        $this->render('index', array(
        'dataProvider' => $dataProvider,
        ));
    }
    public function actionSQL()
    {
    $count=Yii::app()->db->createCommand('SELECT COUNT(*)
    FROM user_group')->queryScalar();
    $sql='SELECT * FROM user_group';
    $dataProvider=new CSqlDataProvider($sql, array(
    'keyField'=>'id',
    'totalItemCount'=>$count,
    'sort'=>array(
    'attributes'=>array('name'),
    'defaultOrder'=>array('name' => false),
    ),
    'pagination'=>array(
    'pageSize'=>10,
    ),
    ));
    $this->render('index', array(
    'dataProvider' => $dataProvider,
    ));
    }
	/**
	 * Displays the contact page
	 */
	public function actionForm($id=null)
	{
        echo Yii::app()->user->getReturnUrl();die;
        $model=new GroupComponent();
        $this->handleAjaxValidation($model);
        if ($id && !($model = GroupComponent::model()->findByPk($id))) {
            $this->redirect(Yii::app()->baseUrl);
        }
        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        if(isset($_POST['GroupComponent']))
        {
            $model->attributes = $_POST['GroupComponent'];
            if($model->save()) {
                $this->redirect(Yii::app()->user->getReturnUrl());
            }
        }

        $this->render('form',array(
            'model'=>$model
        ));
	}
}